/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package lamhnh;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import javax.ejb.Stateless;

/**
 *
 * @author user
 */
@Stateless
public class lamSessionBean implements lamSessionBeanRemote {

    public boolean checkUserNameMethod(String username) {
        boolean check = false;
        try
        {
            Connection con = helper.ConnectionHelper.GetConnection();
            String sql = "select * from Users";
            PreparedStatement p = con.prepareStatement(sql);
            ResultSet rs = p.executeQuery();
            while(rs.next())
            {
                if(!rs.getString("UserName").equals(username))
                {
                    check = false;
                }else
                {
                    check = true;
                    break;
                }
            }
        }catch(Exception e)
        {
            e.printStackTrace();
        }
        return check;
    }

    public boolean deleteMailByUserNameMethod(String username) {
        boolean check = false;
         try
        {
            Connection con = helper.ConnectionHelper.GetConnection();
            String sql = "delete from Mails where UserName=?";
            PreparedStatement p = con.prepareStatement(sql);
            p.setString(1, username);
            int a = p.executeUpdate();
            if(a>0)
            {
                check = true;
            }else
            {
                check = false;
            }
        }catch(Exception e)
         {
            e.printStackTrace();
        }
        return check;
    }

    public boolean deleteUsersActivitiesByUserNameMethod(String username) {
        boolean check = false;
         try
        {
            Connection con = helper.ConnectionHelper.GetConnection();
            String sql = "delete from UsersActivities where UserName=?";
            PreparedStatement p = con.prepareStatement(sql);
            p.setString(1, username);
            int a = p.executeUpdate();
            if(a>0)
            {
                check = true;
            }else
            {
                check = false;
            }
        }catch(Exception e)
         {
            e.printStackTrace();
        }
        return check;
    }

    public boolean deleteUsersByUserNameMethod(String username) {
        boolean check = false;
         try
        {
            Connection con = helper.ConnectionHelper.GetConnection();
            String sql = "delete from Users where UserName=?";
            PreparedStatement p = con.prepareStatement(sql);
            p.setString(1, username);
            int a = p.executeUpdate();
            if(a>0)
            {
                check = true;
            }else
            {
                check = false;
            }
        }catch(Exception e)
         {
            e.printStackTrace();
        }
        return check;
    }

    public boolean updateUsersActivitiesMethod(String username, String pay, boolean sub, String win) {
        boolean check = false;
        try
        {
            Connection con = helper.ConnectionHelper.GetConnection();
            String sql = "update UsersActivities set Payment=?,Submitted=?,Wined=? where UserName=?";
            PreparedStatement p = con.prepareStatement(sql);
            p.setString(1, pay);
            p.setBoolean(2, sub);
            p.setString(3, win);
            p.setString(4, username);
            int a = p.executeUpdate();
            if(a>0)
            {
                check = true;
            }else
            {
                check = false;
            }
        }catch(Exception e)
         {
            e.printStackTrace();
        }
        return check;
    }

    
    // Add business logic below. (Right-click in editor and choose
    // "Insert Code > Add Business Method")
 
}
